<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title>Happy ERP</title>

  <%= stylesheet_link_tag "/javascripts/ext41/resources/css/ext-all.css" %>
  <%= stylesheet_link_tag "ext-patch.css" %>
  <%= javascript_include_tag "ext41/ext-all.js" %>
  <%= javascript_include_tag "ext41/locale/ext-lang-zh_CN.js" %>

  <script type="text/javascript">
      Ext.onReady(function () {
                  Ext.QuickTips.init();
                  var userName = Ext.create('Ext.form.field.Text', {
                      fieldLabel: '用户',
                      name: 'user_name',
                      allowBlank: false,
                      blankText: "用户名不能为空",
                      anchor: '95%',
                      listeners: {
                          scope: this,
                          keypress: function (field, e) {
                              if (e.getKey() == 13) {
                                  loginPwd.focus(true);
                              }
                          }
                      }
                  })

//                  var companyId = Ext.create('Ext.form.field.Text', {
//                      name: 'company_id',
//                      allowBlank: false,
//                      hidden: true,
//                      value: 1
//                  })

                  var password = Ext.create('Ext.form.field.Text', {
                      fieldLabel: '密码',
                      name: 'pwd',
                      inputType: 'password',
                      allowBlank: false,
                      blankText: "登录密码不能为空",
                      anchor: '95%',
                      listeners: {
                          specialkey: function (field, e) {
                              if (e.getKey() == Ext.EventObject.ENTER) {
                                  loginButton.handler();
                              }
                          }
                      }
                  })

                  var company = Ext.create('Ext.form.RadioGroup', {
                      //width : 220,
                      fieldLabel: '公司',
                      //hideLabel:true,
                      //style:'margin-left:100px;margin-top:20px',
                      //name:'financial_management',
                      columns: 2,
                      anchor: '100%',
                      vertical: false,
                      items: [
                          {boxLabel: 'YouDo', inputValue: '1', name: 'company_id'<%if request.subdomains.first.to_s.downcase  == "meso"%>,checked:true<%end%>},
                          {boxLabel: 'ECM', inputValue: '2', name: 'company_id'<%if request.subdomains.first.to_s.downcase  == "ecm"%>,checked:true<%end%>}
                      ]
                  });
                  var loginButton = Ext.create('Ext.Button', {
                      text: '    登  录    ',
                      handler: function () {
                          loginButton.setDisabled(true);
                          userName.setReadOnly(true);
                          password.setReadOnly(true);
                          if (loginForm.form.isValid()) {
                              loginForm.getForm().submit({
                                  success: function (result, resp) {
                                      if (resp.result.info.indexOf("成功") >= 0) {
                                          window.document.location.href = '/';
                                          //window.location.replace('/');
                                      } else {
                                          Ext.MessageBox.show({
                                              title: '消息',
                                              msg: resp.result.info,
                                              buttons: Ext.MessageBox.OK,
                                              icon: Ext.MessageBox.WARNING
                                          });
                                          loginButton.enable();
                                          userName.setReadOnly(false);
                                          password.setReadOnly(false);
                                      }
                                  },
                                  failure: function (result, request) {
                                      loginButton.enable();
                                      Ext.MessageBox.show({
                                          title: '消息',
                                          msg: "登录过程中和服务器的通信失败!",
                                          buttons: Ext.MessageBox.OK,
                                          icon: Ext.MessageBox.WARNING
                                      });
                                  }
                              });
                          }
                          else {
                              loginButton.enable();
                              Ext.MessageBox.show({
                                  title: '消息',
                                  msg: "请输入必输选项",
                                  buttons: Ext.MessageBox.OK,
                                  icon: Ext.MessageBox.WARNING
                              });
                          }
                      }
                  })

                  var loginForm = Ext.create('Ext.form.Panel', {
                      fieldDefaults: {
                          labelWidth: 35,
                          labelAlign: 'right'
                      },
                      url: '/index/validate_user',
                      frame: true,
                      method: 'GET',
                      width: 440,
                      layout: 'anchor',
                      buttonAlign: 'center',
                      style: {
                          border: '0px'
                      },
                      buttons: [loginButton],
                      items: [
                          {
                              id: 'formImg',
                              bodyStyle: Ext.isIE ? 'padding:15px 0 5px 15px;' : 'padding:10px 15px;border:hidden',
                              xtype: 'panel',
                              frame: true,
                              style: {
                                  border: '0px'
                              },
                              html: '<img border=0 src = "/images/welcome<%=@imageNumber%>.jpg" width="400" height="266"/>'
                          },
                          {
                              id: 'formTable',
                              xtype: 'panel',
                              fieldDefaults: {
                                  labelWidth: 70,
                                  labelAlign: 'right'
                              },
                              layout: 'column',
                              style: {
                                  border: '0px'
                              },
                              frame: true,
                              items: [
                                  {
                                      columnWidth: .3,
                                      layout: 'anchor',
                                      xtype: 'container',
                                      items: [userName]
                                  },
                                  {
                                      columnWidth: .3,
                                      layout: 'anchor',
                                      xtype: 'container',
                                      items: [password]
                                  },
                                  {
                                      columnWidth: .4,
                                      layout: 'anchor',
                                      xtype: 'container',
                                      items: [company]
                                  }
                              ]}
                      ]
                  });

                  var loginWin = Ext.create('Ext.window.Window', {
                      closable: false,
                      draggable: false,
                      height: 397,
                      width: 450,
                      style: {
                          border: '0px'
                      },
                      region: 'center',
                      resizable: false,
                      url: '/index/validate_user',
                      items: [loginForm],
                      title: '系统登录'
                  });
                  loginWin.show();
              }
      )
  </script>
</head>
<body>
</body>
</html>